package cz.muni.fi.pa165.sportclubmanager.web;

import cz.muni.fi.pa165.sportclubmanager.dto.PlayerDTO;
import cz.muni.fi.pa165.sportclubmanager.dto.RosterDTO;
import cz.muni.fi.pa165.sportclubmanager.dto.TeamDTO;
import cz.muni.fi.pa165.sportclubmanager.service.PlayerService;
import cz.muni.fi.pa165.sportclubmanager.service.RosterService;
import cz.muni.fi.pa165.sportclubmanager.service.TeamService;
import java.util.List;
import net.sourceforge.stripes.action.*;
import net.sourceforge.stripes.controller.LifecycleStage;
import net.sourceforge.stripes.integration.spring.SpringBean;
import net.sourceforge.stripes.validation.Validate;
import net.sourceforge.stripes.validation.ValidateNestedProperties;

@UrlBinding("/r/{$event}/{team.id}")
public class RostersActionBean implements ActionBean {

    private static final String INDEX = "/rosters/index.jsp";
    private static final String EDIT = "/rosters/edit.jsp";
    private ActionBeanContext context;
    @SpringBean
    protected RosterService rosterService;
    @SpringBean
    protected TeamService teamService;
    @SpringBean
    protected PlayerService playerService;
    @ValidateNestedProperties(value = {
        @Validate(on = {"save", "add"}, field = "number", required = true, minvalue=1, maxvalue=99)
    })
    private RosterDTO roster;
    private TeamDTO team;
    private PlayerDTO player;

    public List<RosterDTO> getRosters() {
        return rosterService.getPlayersOnRoster(team);
    }
    
    public List<PlayerDTO> getAllPlayers() {
        List<PlayerDTO> list = rosterService.getPlayersAvailableForRoster(team);
        for(PlayerDTO player : list){
            player.setLastName(player.getLastName() + ", " + player.getFirstName());
        }
        return list;
    }
    
    @Before(stages = LifecycleStage.BindingAndValidation, on = {"delete"})
    public void beforeDelete(){
        roster = new RosterDTO();
        String ids = context.getRequest().getParameter("team.id");
        if (ids != null) {
            team = teamService.find(Long.parseLong(ids));
        }
        ids = context.getRequest().getParameter("id");
        if (ids != null) {
            roster.setId(Long.parseLong(ids));
        }
    }
    
    @Before(stages = LifecycleStage.BindingAndValidation, on = {"save"})
    public void beforeSave(){
        roster = new RosterDTO();
        String ids = context.getRequest().getParameter("number");
        if (ids != null) {
            roster.setNumber(Integer.parseInt(ids));
            ids = context.getRequest().getParameter("id");
            if (ids != null) {
                roster.setId(Long.parseLong(ids));
            }
        }
    }

    @Before(stages = LifecycleStage.BindingAndValidation, on = {"index", "add"})
    public void before() {
        String ids = context.getRequest().getParameter("team.id");
        if (ids != null) {
            team = teamService.find(Long.parseLong(ids));
        }
    }
    
    @Before(stages =  LifecycleStage.BindingAndValidation, on = {"edit"})
    public void beforeEdit(){
        String ids = context.getRequest().getParameter("team.id");
        if (ids != null) {
            team = teamService.find(Long.parseLong(ids));
        }
        ids = context.getRequest().getParameter("player.id");
        if (ids != null) {
            player = playerService.find(Long.parseLong(ids));
            player.setLastName(player.getLastName() + ", " + player.getFirstName());
        }
        ids = context.getRequest().getParameter("number");
        if (ids != null) {
            roster = new RosterDTO();
            roster.setNumber(Integer.parseInt(ids));
            ids = context.getRequest().getParameter("id");
            if (ids != null) {
                roster.setId(Long.parseLong(ids));
            }
        }
    }

    @DefaultHandler
    public Resolution index() {
        return new ForwardResolution(INDEX);
    }

    public Resolution save() {
        try{
            rosterService.changeNumber(roster);
            getContext().getMessages().add(new SimpleMessage("number changed"));
        } catch (Exception ex) {
            getContext().getMessages().add(new SimpleMessage("error: " + ex.getLocalizedMessage()));
        }                
        return new RedirectResolution(this.getClass(), "index").addParameter("team.id", team.getId());
    }
    
    
    public Resolution edit() {
        return new ForwardResolution(EDIT);
    }
    
    public Resolution add() {
        try{
            PlayerDTO player = playerService.find(roster.getPlayer().getId());
            rosterService.addPlayerToRoster(team, player, roster.getNumber());
            getContext().getMessages().add(new SimpleMessage("player added"));
        } catch (Exception ex) {
            getContext().getMessages().add(new SimpleMessage("error: " + ex.getLocalizedMessage()));
        }
        return new RedirectResolution(this.getClass(), "index").addParameter("team.id", team.getId());
    }

    public Resolution delete() {
        try{
            rosterService.removePlayerFromRoster(roster);
            getContext().getMessages().add(new SimpleMessage("player removed"));
        } catch (Exception ex) {
            getContext().getMessages().add(new SimpleMessage("error: " + ex.getLocalizedMessage()));
        }
        return new RedirectResolution(this.getClass(), "index").addParameter("team.id", team.getId());
    }

    
    @Override
    public void setContext(ActionBeanContext context) {
        this.context = context;
    }

    @Override
    public ActionBeanContext getContext() {
        return context;
    }

    public TeamDTO getTeam() {
        return team;
    }

    public void setTeam(TeamDTO team) {
        this.team = team;
    }

    public void setRoster(RosterDTO roster){
        this.roster = roster;
    }
    
    public RosterDTO getRoster(){
        return roster;
    }

    public PlayerDTO getPlayer() {
        return player;
    }

    public void setPlayer(PlayerDTO player) {
        this.player = player;
    }
}